package com.tanhua.server.controller;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author lifeihu
 * @date 2021/12/23
 * @TIME:18:14
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
   private UserInfoService userInfoService;
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token){
        //1、判断token是否合法
       /* boolean flag = JwtUtils.verifyToken(token);
        if(!flag){
            return ResponseEntity.status(401).body(null);
        }*/
        //2、向userinfo中设置用户id
        Long id = UserHolder.get().getId();
        userInfo.setId(id);
        //3、调用service
       userInfoService.save(userInfo);
       return ResponseEntity.ok(null);
    }
    @PostMapping("/loginReginfo/head")
    public ResponseEntity head(MultipartFile headPhoto, @RequestHeader("Authorization") String token){
       /* boolean flag = JwtUtils.verifyToken(token);
        if(!flag){
            return ResponseEntity.status(401).body(null);
        }
        Claims claims = JwtUtils.getClaims(token);
        Long id = Long.valueOf(claims.get("id").toString());*/
        Long id = UserHolder.get().getId();
        //3、调用service
        try {
            userInfoService.updateHead(headPhoto, id);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok(null);
    }
}
